package com.byxy.aspect;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import com.byxy.entity.Log;
import com.byxy.entity.User;
import com.byxy.service.LogService;

//@Controller
//@Repository
//@Service
@Component
@Aspect
public class LogAspect {

	@Resource
	private LogService logService;

	@Pointcut("execution (* com.byxy.action.*.*(..))")
	public void userLogin() {
	}

	@Pointcut("@annotation(com.byxy.aspect.MyLog)")
	public void logPointCut() {
	}

	// @Before("userLogin()")
	// public void doBefore() {
	// System.out.println("+++++++++++++++++++++++++++");
	// }
	//
	// @AfterReturning("userLogin()")
	// public void doAfterReturning() {
	// System.out.println("############################");
	// }

	@Before("userLogin() && @annotation(myLog)")
	public void before(JoinPoint joinPoint, MyLog myLog) {
		HttpServletRequest request = ServletActionContext.getRequest();
		// 获取用户登录名
		User u = (User) request.getSession().getAttribute("user");
		if (u != null) {
			// 获取切入的方法名
			String methodName = joinPoint.getSignature().getName();
			Log log = new Log();
			log.setDate(new Date());
			log.setId((int) new Date().getTime());
			log.setDescribes(myLog.desc());
			log.setOperatorName(u.getName());
			log.setRemark(methodName);
			logService.save(log);
		}
	}

}
